home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -in_the_mag- / program_perfection / vbcc / mystartup / _main.c next >
C/C++ Source or Header  |  2000-03-11  |  1KB  |  80 lines

  1. /*
  2.  * _main.c
  3.  *
  4.  * Startup stuff for VBCC
  5.  * Handles calling of constructors, destructors and atexit() functions
  6.  *
  7.  * Basically just pinched from vc.lib :)
  8.  *
  9.  * (C) Richard Drummond 1999
  10.  *
  11.  * Please feel free to use and abuse this code in your own
  12.  * programs, but don't blame me if it goes wrong . . .
  13.  *
  14.  * $Id: _main.c,v 1.2 1999/11/10 22:44:24 richard Exp $
  15.  * $Log: _main.c,v $
  16.  * Revision 1.2  1999/11/10 22:44:24  richard
  17.  * Update
  18.  *
  19.  * Revision 1.1.1.1  1999/11/10 22:38:28  richard
  20.  * Initial import to cvs
  21.  *
  22.  */
  23.  
  24.  
  25. #include <stddef.h>
  26. #include <stdlib.h>
  27.  
  28.  
  29. extern       int  main( char *, int );
  30. extern __far void (*_ctors[])(), (*_dtors[])();
  31.  
  32. struct __exitfuncs *__firstexit;
  33.  
  34. void   _exit( __reg("d0") int );
  35.  
  36.  
  37. void
  38. _main( char *command, int len )
  39. {
  40.     void (**ctors)() = _ctors;
  41.  
  42.     /* call constructors */
  43.     if( ctors++ )
  44.         while( *ctors ) (*ctors++)();     
  45.  
  46.     exit( main( command, len ) );
  47. }
  48.  
  49.  
  50.  
  51. void exit( int returncode )
  52. {
  53.     void (**dtors)() = _dtors;
  54.     int i;
  55.  
  56.     struct  __exitfuncs *p=__firstexit;
  57.  
  58.     /* call atexit() functions */
  59.     while( p )
  60.     {
  61.         p->func();
  62.         p = p->next;
  63.     }
  64.  
  65.     if( dtors )
  66.     {
  67.         /* goto end of destructor list */
  68.         for( i = 1; *dtors[i]; i++ )
  69.             ;
  70.  
  71.         /* call destructors in reverse */
  72.         while( --i )
  73.             (*dtors[i])();
  74.     }
  75.  
  76.     _freemem();
  77.     _exit( returncode );
  78. }
  79.  
  80.